package cn.lhjl.day08.JDBC;

import cn.lhjl.day08.Utils.JdbcUtils;

import javax.swing.text.Utilities;
import java.sql.*;
import java.util.Scanner;

/*
 *1.在oralce中创建一张账户密码表
 *2.用户输入账户密码。
 *3.根据用户的输入在表中查询
 *4.如果根据返回结果来判断用户的输入匹配，显示成功或失败
 *
 *
 *
 * */
public class Jdbc实现账户登录验证 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入id");
        //这里使用nextline,next遇到空格会停止接受键盘的输入；
        String id = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        boolean flag = login(id, password);
        if (flag) System.out.println("登录成功");
        else System.out.println("登录失败");
    }

    public static boolean login(String id, String password) {
        //数据库驱动加载、创建连接对象、创建执行sql语句对象、执行sql语句返回ResultSet集合，根据next（）方法判断
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnect();
            String sql = "select * from mypassword where id=? and mypassword =?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(rs, pstmt, conn);
        }
        return false;

    }
}
